package com.ht.service.impl;

import com.ht.data.Result;
import com.ht.domin.po.Praise;
import com.ht.service.PraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PraiseServiceImpl implements PraiseService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Result findPraiseByUserName(String userName) {
        try {
            List<Praise> praiseList = mongoTemplate.find(new Query(Criteria.where("user").regex(userName)), Praise.class);
            return Result.OK().setData(praiseList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.ERROR();
    }

    @Override
    public Result findPraiseByUserId(Long id) {
        try {
            List<Praise> praiseList = mongoTemplate.find(new Query(Criteria.where("user").regex(String.valueOf(id))), Praise.class);
            return Result.OK().setData(praiseList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.ERROR();
    }
}
